package com.donfu.web.controller.wxhouse;

import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.system.domain.house.HouseCart;
import com.donfu.system.service.house.IHouseCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Auther: 覃创斌
 * @Date: 2021/12/7 20:48
 * @Description: 购物车功能
 */
@RestController
@RequestMapping("/wx/shoppingcart")
public class WebShoppingCartController extends BaseController {

    @Autowired
    private IHouseCartService houseCartService;


    /*
     * @作者 覃创斌
     * @描述  加入购物车
     * @日期 2021/12/7
     **/
    @PostMapping("/joinCartList")
    public AjaxResult joinCartList(HouseCart houseCart)
    {
        try {
            houseCart.setAddTime(new Date());
            houseCartService.insertHouseCart(houseCart);
            return AjaxResult.success("已成功加入购物车");
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("加入购物车失败");
        }

    }

    /*
    * @作者 覃创斌
    * @描述  查询购物车列表
    * @日期 2021/12/7
    **/
    @PostMapping("/getCartList")
    public AjaxResult getCartList(@RequestParam(value="userId",required = true ) Long userId)
    {
        HouseCart houseCart = new HouseCart();
        houseCart.setUserId(userId);
        List<HouseCart> houseCartList = houseCartService.selectHouseCartList(houseCart);
        return AjaxResult.success(houseCartList);
    }
    /*
    * @作者 覃创斌
    * @描述 选中的商品增加或者减少商品数量
    * @日期 2021/12/7
    **/
    @PostMapping("/addGoodsNum")
    public AjaxResult addGoodsNum(Long cartId,Long userId,Long goodsId,Integer number)
    {
        HouseCart houseCart = new HouseCart();
        houseCart.setUserId(userId);
        houseCart.setId(cartId);
        houseCart.setGoodsId(goodsId);
        houseCart.setNumber(number);
        houseCartService.updateHouseCart(houseCart);
        return AjaxResult.success();
    }
    /*
    * @作者 覃创斌
    * @描述 清空购物车
    * @日期 2021/12/8
    **/
    @PostMapping("/emptyCart")
    public AjaxResult emptyCart(Long userId)
    {
        HouseCart houseCart = new HouseCart();
        houseCart.setUserId(userId);
        houseCartService.emptyCart(userId);
        return AjaxResult.success("清空成功");
    }
    /*
     * @作者 覃创斌
     * @描述 删除购物车中的商品
     * @日期 2021/12/8
     **/
    @PostMapping("/delGoods")
    public AjaxResult delGoods(Long id)
    {
        houseCartService.deleteHouseCartById(id);
        return AjaxResult.success("删除成功");
    }
}
